package com.jsjosem.aftersales.ui.worktickets;

import static com.jsjosem.aftersales.utils.CommonUtils.getDepartmentsMenu;
import static com.jsjosem.aftersales.utils.CommonUtils.getDepartmentsMultipleChoiceMenu;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.adapter.HelperListAdapter;
import com.jsjosem.aftersales.api.RetrofitClient;
import com.jsjosem.aftersales.api.WorkOrderApi;
import com.jsjosem.aftersales.data.request.AllotWorkRequest;
import com.jsjosem.aftersales.data.response.BaseResponse;
import com.jsjosem.aftersales.data.response.DepartmentsUserResponse;
import com.jsjosem.aftersales.request.AsyncRequest;
import com.jsjosem.aftersales.utils.ViewUtils;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import retrofit2.Call;
import retrofit2.Retrofit;

public class SelectUserActivity extends AppCompatActivity {

    private ImageView directoryImg;
    private ConstraintLayout helperLay, workOrderPersonLay;
    private ListView helperName;
    private TextView staffName;

    private DepartmentsUserResponse masterUser;
    private List<DepartmentsUserResponse> helperList;
    private HelperListAdapter helperAdapter;
    private Button btSubmit;
    private Button btCancel;
    private Dialog loadingDialog;
    private Retrofit retrofit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_user);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String workId = bundle.getString("workId");
        masterUser = new DepartmentsUserResponse();
        helperList = new ArrayList<>();
        retrofit = RetrofitClient.getInstance();
        helperAdapter = new HelperListAdapter(this, R.layout.product_item_list, helperList);
        helperName.setAdapter(helperAdapter);

        //选择区域负责人
        workOrderPersonLay.setOnClickListener(view -> {
            getDepartmentsMenu(this, directoryImg, staffName, masterUser);

        });

        //选择协助人
        helperLay.setOnClickListener(view -> {
            getDepartmentsMultipleChoiceMenu(this, helperName, helperList);
            helperAdapter.notifyDataSetChanged();
        });

        //点击删除协助人
        helperName.setOnItemClickListener((adapterView, view, i, l) -> {
            helperList.remove(i);
            helperAdapter.notifyDataSetChanged();
            ViewUtils.setListViewHeightBasedOnChildren(helperName);
        });

        // 取消
        btCancel.setOnClickListener(view -> {
            // 关闭当前页面
            finish();
        });

        // 提交
        btSubmit.setOnClickListener(view -> {
            if (Objects.isNull(masterUser)) {
                Toast.makeText(this, "请选择负责人后再提交", Toast.LENGTH_SHORT).show();
                return;
            }
            loadingShow();
            WorkOrderApi workOrderApi = retrofit.create(WorkOrderApi.class);
            AllotWorkRequest allotWorkRequest = new AllotWorkRequest();
            allotWorkRequest.setmUserId(masterUser.getId());
            if (CollUtil.isNotEmpty(helperList)) {
                List<Long> helperIds = helperList.stream().map(DepartmentsUserResponse::getId).collect(Collectors.toList());
                allotWorkRequest.setmAssistUserIds(helperIds);
            }
            Call<BaseResponse<Void>> allotOrderCall = workOrderApi.allotOrder(workId, allotWorkRequest);
            AsyncRequest<BaseResponse<Void>> request = new AsyncRequest<>();
            request.sendRequest(allotOrderCall, (res) -> {
                loadingDialog.dismiss();
                Toast.makeText(this, res.getMsg(), Toast.LENGTH_SHORT).show();
                this.setResult(RESULT_OK, intent);
                finish();
            }, errMsg -> {
                loadingDialog.dismiss();
                Toast.makeText(this, errMsg.toString(), Toast.LENGTH_SHORT).show();
            });
        });
    }

    private void initView() {
        directoryImg = findViewById(R.id.work_order_person_img);
        helperName = findViewById(R.id.work_order_helper_name);
        helperLay = findViewById(R.id.work_order_helper_item);
        workOrderPersonLay = findViewById(R.id.work_order_person_item);
        staffName = findViewById(R.id.work_order_person_name);
        btCancel = findViewById(R.id.bt_cancel);
        btSubmit = findViewById(R.id.bt_submit);
    }


    public void loadingShow() {
        loadingDialog = new Dialog(this, R.style.normal_dialog_style);
        View loadingView = View.inflate(this, R.layout.dialog_loading, null);
        loadingDialog.setContentView(loadingView);
        //使得点击对话框外部不消失对话框
        loadingDialog.setCanceledOnTouchOutside(false);
        AVLoadingIndicatorView avi = loadingView.findViewById(R.id.avi);
        avi.show();
        loadingDialog.show();
    }
}